//
//  GreetConfigView.m
//  ExtModule
//
//  Created by CPX on 2018/9/10.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "GreetConfigView.h"

/**
 祈福设置View
 */
@interface GreetConfigView ()
{
    NSInteger _index;
}
@property (nonatomic,strong)NSArray *normalImages;
@property (nonatomic,strong)NSArray *selectImages;
@property (nonatomic,strong)NSArray *titles;

@property (nonatomic,strong)NSMutableArray * buttonArray;
@end

@implementation GreetConfigView

- (instancetype)initWithFrame:(CGRect)frame
                       titles:(NSArray<NSString *> *)titles
                  normalImage:(NSArray<UIImage *> *)normalImages
                 selectImages:(NSArray<UIImage *> *)selectImages
{
    self = [super initWithFrame:frame];
    if (self) {
        self.normalImages = normalImages;
        self.selectImages = selectImages;
        self.titles =  titles;
    
        [self initUI];
    }
    return self;
}

- (NSMutableArray *)buttonArray{
    if(!_buttonArray){
        _buttonArray  = [NSMutableArray new];
    }
    return _buttonArray;
}

-(void)initUI{
    CGFloat margin =  10;
  
    CGFloat width_button = (self.bounds.size.width - self.normalImages.count * margin)/self.normalImages.count;
    for (NSInteger index = 0 ;index<self.normalImages.count;index++){
        StateButton * button  = [[StateButton alloc]init];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(index * (width_button + margin) + margin);
            make.width.mas_equalTo(width_button);
            make.centerY.equalTo(self);
            make.top.mas_equalTo(5);
        }];
        if (self.titles){
            [button setTitle:self.titles[index] forState:UIControlStateNormal];
        }
        if (self.selectImages){
            [button setImage:self.selectImages[index] forState:UIControlStateSelected];
        }
        if (self.normalImages){
            [button setImage:self.normalImages[index] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(onTouchButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttonArray addObject:button];
    }
    UIView * line = [UIView new];
    [self addSubview:line];
    line.backgroundColor = [UIColor lightGrayColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.leading.trailing.top.equalTo(self);
        
    }];
    
}

-(void)onTouchButton:(UIButton *)sender{
    
    NSInteger index  = [self.buttonArray indexOfObject:sender];
    if (index == _index){
        return;
    }
    [self.buttonArray enumerateObjectsUsingBlock:^(UIButton *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
    }];
    sender.selected = YES;
    _index = index;
    self.touchIndexEvent?self.touchIndexEvent(_index):nil;
    
    
}
- (void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex = selectIndex;
    [self.buttonArray enumerateObjectsUsingBlock:^(UIButton *  obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == selectIndex){
            obj.selected = YES;
        }
        else{
            obj.selected = NO;
        }
    }];
    _index = selectIndex;
}

@end


/**
 点击自动被选中select的button
 */
@implementation StateButton
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        self.shouldAutoSelect = YES;
        [self addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self  = [super initWithCoder:aDecoder]){
        self.shouldAutoSelect = YES;
        [self addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}
#pragma mark ----去掉高亮效果
-(void)setHighlighted:(BOOL)highlighted{
    
}
#pragma mark --- 点击就被选中
-(void)buttonClick:(UIButton* )sender{
    if (self.shouldAutoSelect){
        sender.selected = !sender.selected;
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if ([self nextResponder]){
        [[self nextResponder] touchesBegan:touches withEvent:event];
    }
}


@end
